package com.ironman.analyze.core.compute;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.analyze.core.compute.base.BaseAverageComputer;
import com.ironman.analyze.core.compute.base.BaseComputer;
import com.ironman.analyze.core.compute.entity.SpeedWindowData;
import com.ironman.analyze.core.context.TrainingContext;
import com.ironman.analyze.core.handler.SpeedPostHandler;
import com.ironman.common.entity.SpeedData;
import com.ironman.common.params.SpeedParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

@Slf4j
public class SpeedAverageComputer extends BaseAverageComputer<SpeedData> implements BaseComputer<SpeedData, SpeedWindowData> {

    private final SpeedPostHandler speedPostHandler;

    public SpeedAverageComputer(TrainingContext trainingContext, SpeedParam speedParam) {
        super(speedParam.getWindowSize(), trainingContext);
        this.speedPostHandler = new SpeedPostHandler(speedParam);
    }

    @Override
    public void computeWindowData(List<SpeedData> dataList) {
        for (SpeedData speedData : dataList) {
            windowData(speedData, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double speed) {
                    SpeedWindowData speedWindowData = new SpeedWindowData(ts, speed);
                    speedPostHandler.handler(speedWindowData, new Consumer<SpeedWindowData>() {
                        @Override
                        public void accept(SpeedWindowData speedWindowData) {
                            syncData(speedWindowData);
                        }
                    });
                }
            });
        }
    }

    @Override
    public void syncData(SpeedWindowData windowData) {
        getTrainingContext().getSpeedWindowDataMap().put(windowData.getTs(), windowData);
    }

    public static void main(String[] args) {
        SpeedAverageComputer speedComputer = new SpeedAverageComputer(null, null);

        List<SpeedData> speedDataList = ListUtil.of(
                new SpeedData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 172.5467D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 131.9738D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 139.6231D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 139.6231D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 149.9448D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 180.8973D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 106.9091D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 129.4654D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 200.2282D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 119.9718D)
        );
        speedComputer.computeWindowData(speedDataList);
    }


}
